﻿using ConfExpert.Datasets;
using ConfExpert.Utility;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevTeam;

using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Westbrook
{
    public partial class BedenDagilim : XPopupForm
    {
        string DosyaNo;
        public BedenDagilim(int DagilimId, string DosyaNo)
        {
            InitializeComponent();
            this.DagilimId = DagilimId;
            this.DosyaNo = DosyaNo;
        }
        string bedenDosyaNo;
        private void BedenDagilim_Load(object sender, EventArgs e)
        {
            txtInfo.Caption = DosyaNo;
            DataTable dtBeden = ExecuteDataTable("GetBedenDagilimlari", DosyaNo);
            gridView1.Init(dtBeden);
            if (dtBeden.Rows.Count > 0)
                bedenDosyaNo = dtBeden.Rows[0]["DosyaNo"].ToString();
            else
                bedenDosyaNo = DBNull.Value.ToStr();
            gridView1.MakeColumnInvisible("DagilimId", "DosyaNo", "BedenDagilimId");
            if (gridView1.DataTable().Rows.Count == 0)
                gridView1.DataTable().Rows.Add(gridView1.DataTable().NewRow());
            gridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;
            gridView1.OptionsView.ShowAutoFilterRow = false;
            DagilimChanged();
            gridView1.BestFitColumns();
            DataTable dt = ExecuteDataTable("GetDagilimEsleme", WhoAmI);

            foreach (DataRow row in dt.Rows)
            {
                ((RepositoryItemComboBox)cmbEsleme.Edit).Items.Add(row["Name"]);
            }

            barButtonItem1.ItemClick += barButtonItem1_ItemClick;
            btnEsleme.ItemClick += btnEsleme_ItemClick;
            ((RepositoryItemComboBox)cmbEsleme.Edit).SelectedValueChanged += BedenDagilim_SelectedValueChanged;

        }

        void BedenDagilim_SelectedValueChanged(object sender, EventArgs e)
        {
            barManager.ActiveEditItemLink.PostEditor();
            string cmb = cmbEsleme.EditValue.ToStr();
             DataTable dt;
             if (bedenDosyaNo != "")
                 dt = ExecuteDataTable("GetDagilimEslemeSelected", cmb, bedenDosyaNo, DagilimId);
             else
                 dt = ExecuteDataTable("GetDagilimEslemeSelected", cmb, DBNull.Value, DagilimId);
            gridView1.Init(dt);
            gridView1.MakeColumnInvisible("DagilimEslemeId","UserId", "DosyaNo","DagilimId");
            gridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;
            gridView1.OptionsView.ShowAutoFilterRow = false;
            DagilimChanged();
            gridView1.BestFitColumns();
        }


        void btnEsleme_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            DataTable result = ExecuteDataTable("InsDagilimEsleme", txtEsleme.EditValue, WhoAmI, gridView1.SerializeChanges());
            btnEsleme.Enabled = false;
            XtraMessageBox.Show("Beden dağılım eşlemeniz kaydedilmiştir.");
        }

        void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            DataTable t = gridView1.DataTable();
            string a = gridView1.SerializeChanges();
            string b = gridView1.Serialize();
            DataTable result = ExecuteDataTable("UpdBedenDagilimlari", DosyaNo, DagilimId, gridView1.Serialize());
            barButtonItem1.Enabled = false;
            this.Close();
        }

        private void DagilimChanged()
        {
            MyData.DagilimRow r = AppSession.Lookups.Dagilim.FindByDagilimId(DagilimId);
            if (r != null)
                for (int i = 1; i <= 28; i++)
                {
                    string fieldName = String.Format("A{0:00}", i);
                    string cap = r[String.Format("A{0:00}", i)].ToString();

                    if (cap != "")
                    {
                        gridView1.Columns[fieldName].Caption = cap;
                    }
                    else
                        gridView1.Columns[fieldName].Caption = " ";
                }

        }

        public int DagilimId { get; set; }

        private void BedenDagilim_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            bool res = false;
            if (barButtonItem1.Enabled)
                res = XtraMessageBox.Show("Beden dağılımlarını kaydetmediniz. Formu kapatmak istediğinize emin misiniz?", "Uyarı", MessageBoxButtons.YesNo) != DialogResult.Yes;
            e.Cancel = res;
        }
    }
}
